package com.hd.trans.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

import com.hd.trans.R;

/**
 *
 * Author: yang
 * Date: 2019/1/22 15:21
 * Desc: 可以设置图片大小的TextView
 *
 */
public class ImageTextView extends AppCompatTextView {

    private int mImageWidth = 0;
    private int mImageHeight = 0;

    public ImageTextView(Context context) {
        this(context,null);
    }

    public ImageTextView(Context context, AttributeSet attrs) {
        this(context, attrs,android.R.attr.textViewStyle);
    }

    public ImageTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init( attrs );
    }

    private void init(AttributeSet attrs) {

//    	mActContent = context;
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ImageTextView);

        mImageWidth = typedArray.getDimensionPixelSize(R.styleable.ImageTextView_image_width, mImageWidth );
        mImageHeight = typedArray.getDimensionPixelSize(R.styleable.ImageTextView_image_height, mImageHeight);

        typedArray.recycle();

        Drawable[] drawables = getCompoundDrawables();
        for ( Drawable drawable:drawables ){
            if(drawable!=null){
                int width = mImageWidth>0 ? mImageWidth : drawable.getIntrinsicWidth();
                int height = mImageHeight>0 ? mImageHeight : drawable.getIntrinsicHeight();
                drawable.setBounds(0, 0, width, height );
            }
        }
        setCompoundDrawables( drawables[0], drawables[1], drawables[2], drawables[3] );
    }
}
